package network.tcp.nio.http;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @program: bio
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-27 15:48
 **/
public class HttpServer {
    public static void main(String[] args) {
        //首先是一个nio的server
        // 需要默写nio服务器
        server();
    }

    private static void server() {
        try (ServerSocketChannel channel = ServerSocketChannel.open();) {

            //创建channel
            SocketAddress local = new InetSocketAddress(10000);
            // channel.bind(local);

            //绑定
            channel.bind(local);

            //设置非阻塞
            channel.configureBlocking(false);

            //绑定通道
            Selector selector = Selector.open();

            //注册选择器,和选择器类型
            channel.register(selector, SelectionKey.OP_ACCEPT);

            //设置了非
            while (true) {
                // System.out.println("外层循环中");
                //判断是否有客户端连接
                int select = selector.select();
                // System.out.println("2222");
                if (select > 0) {
                    System.out.println("有客户端连接");
                    //循环,修改状态,
                    //会遍历所有的服务端通道.看谁准备好了,谁准备好了,就让谁去连接
                    Set<SelectionKey> selectionKeys = selector.selectedKeys();
                    Iterator<SelectionKey> selectionKeyIterator = selectionKeys.iterator();
                    while (selectionKeyIterator.hasNext()) {
                        //获取令牌,进行循环
                        SelectionKey selectionKey = selectionKeyIterator.next();
                        //如果是只读的,就改成改改状态
                        if (selectionKey.isAcceptable()) {
                            //修改channel,可用后就进行通信
                            System.out.println("===isAcceptable===");
                            AcceptHandler handler = new AcceptHandler();
                            handler.connectSocketChannel(selectionKey);

                        } else if (selectionKey.isReadable()) {
                            System.out.println("===isReadable===");
                            //获取请求信息,封装
                            HttpRequest request = new HttpRequest();
                            request.parse(selectionKey);

                            if (request.getRequestURI() == null ||
                                    "".equals(request.getRequestURI())) {
                                selectionKey.channel();
                                continue;
                            }
                            //返回请求体信息
                            HttpResponse httpResponse = new HttpResponse(request);
                            httpResponse.sendStaticResource(selectionKey);

                        }
                        selectionKeyIterator.remove();
                    }

                }
                System.out.println("=========外层循环一次========");
            }
            //    循环获取连接

            //获取接口的信息,

            //读取到消息

            //释放资源   加入到 try的小括号中
            // channel.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }
}
